package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Properties;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.internal.dnd.SwtUtil;

import data.ShowData;

import views.SelectedView;

public class ExportHandler extends AbstractHandler
{
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException
    {
        File file = openFileDialog();
        if (file == null)
            return null;

        exportShows(file);
        return null;
    }


    private File openFileDialog()
    {
        FileDialog fd = new FileDialog(Display.getCurrent().getShells()[0], SWT.SAVE);
        fd.setFilterExtensions(new String[]{"*.xml"});
        String fName = fd.open();
        if (fName == null)
            return null; 
        
        return new File(fName);
    }
    
    private void exportShows(File file)
    {
        Properties p = new Properties();
        LinkedList<ShowData> shows = SelectedView.getInstance().getShows();
        for (ShowData showData : shows)
        {
            //title,location,newname
            StringBuilder sb = new StringBuilder();
            sb.append(showData.getTitle());
            sb.append(",");
            sb.append(showData.getFileName());
            sb.append(",");
            sb.append(showData.getNewFileName());
            p.setProperty(showData.getShowId(), sb.toString());
        }
        
        try
        {
            p.storeToXML(new FileOutputStream(file), null);
        } catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private void importShows(File file)
    {
        Properties p = new Properties();
        LinkedList<String> showsIds = new LinkedList<String>();
        try
        {
            p.load(new FileInputStream(file));
            Enumeration<Object> keys = p.keys();
            while (keys.hasMoreElements())
            {
                String key = (String) keys.nextElement();
                showsIds.add(key);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        
        LinkedList<ShowData> shows = SageTvShows.getInstance().getShowsDataByIds(showsIds);
        SelectedView.getInstance().addShows(shows);
    }
}
